package com.yunji.product.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.product.bo.ProductAgentListQueryBO;
import com.yunji.product.bo.ProductAgentSelectQueryBO;
import com.yunji.product.bo.ProductAgentUpdateBO;
import com.yunji.product.dao.IProductAgentDao;
import com.yunji.product.domain.ProductAgentDO;
import com.yunji.product.mapper.ProductAgentMapper;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

@Repository
public class ProductAgentDaoImpl extends BaseDaoImpl<ProductAgentMapper, ProductAgentDO> implements IProductAgentDao {
    @Override
    public boolean updateByBo(ProductAgentUpdateBO bo) {
        return update(new LambdaUpdateWrapper<ProductAgentDO>()
                .set(BeanUtils.isNotNull(bo.getStatus()), ProductAgentDO::getStatus, bo.getStatus())
                .set(BeanUtils.isNotNull(bo.getChangeStatus()), ProductAgentDO::getChangeStatus, bo.getChangeStatus())
                .set(BeanUtils.isNotNull(bo.getAgentPrice()), ProductAgentDO::getAgentPrice, bo.getAgentPrice())
                .set(BeanUtils.isNotNull(bo.getManagerPrice()), ProductAgentDO::getManagerPrice, bo.getManagerPrice())
                .set(BeanUtils.isNotNull(bo.getDiscountPrice()), ProductAgentDO::getDiscountPrice, bo.getDiscountPrice())
                .eq(BeanUtils.isNotNull(bo.getProductId()), ProductAgentDO::getProductId, bo.getProductId())
                .eq(BeanUtils.isNotNull(bo.getAgentId()), ProductAgentDO::getAgentId, bo.getAgentId())
                .eq(BeanUtils.isNotNull(bo.getBelongId()), ProductAgentDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getManagerId()), ProductAgentDO::getSiteId, bo.getManagerId())
                .eq(BeanUtils.isNotNull(bo.getUserType()), ProductAgentDO::getUserType, bo.getUserType()));
    }

    @Override
    public List<ProductAgentDO> search(ProductAgentListQueryBO bo) {
        return list(new LambdaUpdateWrapper<ProductAgentDO>()
                .eq(BeanUtils.isNotNull(bo.getProductId()), ProductAgentDO::getProductId, bo.getProductId())
                .eq(BeanUtils.isNotNull(bo.getAgentId()), ProductAgentDO::getAgentId, bo.getAgentId())
                .eq(BeanUtils.isNotNull(bo.getManagerId()), ProductAgentDO::getSiteId, bo.getManagerId())
                .eq(BeanUtils.isNotNull(bo.getBelongId()),ProductAgentDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getUserType()), ProductAgentDO::getUserType, bo.getUserType())
                .eq(BeanUtils.isNotNull(bo.getStatus()), ProductAgentDO::getStatus, bo.getStatus())
                .eq(BeanUtils.isNotNull(bo.getUserType()), ProductAgentDO::getUserType, bo.getUserType())
        );
    }

    @Override
    public ProductAgentDO select(ProductAgentSelectQueryBO bo) {
        return getOne(new LambdaQueryWrapper<ProductAgentDO>()
                .eq(BeanUtils.isNotNull(bo.getBelongId()), ProductAgentDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getAgentId()), ProductAgentDO::getAgentId, bo.getAgentId())
                .eq(BeanUtils.isNotNull(bo.getUserType()), ProductAgentDO::getUserType, bo.getUserType())
                .eq(BeanUtils.isNotNull(bo.getStatus()), ProductAgentDO::getStatus, bo.getStatus())
                .eq(BeanUtils.isNotNull(bo.getProductId()), ProductAgentDO::getProductId, bo.getProductId())
                .eq(BeanUtils.isNotNull(bo.getMangerId()), ProductAgentDO::getSiteId, bo.getMangerId()));
    }

    @Override
    public List<String> selectAgentIdByProductId(Long... ids) {
        return listObjs(new LambdaQueryWrapper<ProductAgentDO>()
                .select(ProductAgentDO::getAgentId)
                .in(ProductAgentDO::getProductId, Arrays.asList(ids))
                .eq(ProductAgentDO::getStatus, LogicCode.NO)
                .eq(ProductAgentDO::getUserType, UserType.AGENT.getCode()),Object::toString);
    }

    @Override
    public boolean deleteByManagerIds(Long... managerIds) {
        return remove(new LambdaUpdateWrapper<ProductAgentDO>()
                .in(ProductAgentDO::getSiteId, Arrays.asList(managerIds)));
    }

    @Override
    public boolean deleteByProductIds(Long[] ids) {
        return remove(new LambdaUpdateWrapper<ProductAgentDO>()
                .in(ProductAgentDO::getProductId, Arrays.asList(ids)));
    }

    @Override
    public ProductAgentDO getCacheByBelong(Long belongId, Long productId) {
        return lambdaQuery().eq(ProductAgentDO::getBelongId, belongId).eq(ProductAgentDO::getProductId, productId).one();
    }

    @Override
    public List<ProductAgentDO> getCacheByBelong(Long belongId, List<Long> productIds) {
        return lambdaQuery().eq(ProductAgentDO::getBelongId, belongId).in(ProductAgentDO::getProductId, productIds).list();
    }

//    @Override
//    public Integer countProduct(ProductAgentCountBO bo) {
//        Long count = lambdaQuery().eq(BeanUtils.isNotNull(bo.getBelongId()),ProductAgent::getBelongId,bo.getBelongId())
//                        .eq(BeanUtils.isNotNull(bo.getStatus()),ProductAgent::getStatus,bo.getStatus()).count();
//        return Optional.fromNullable(count).or(0L).intValue();
//    }
}
